package com.atguigu.eduService.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduService.client.VodClient;
import com.atguigu.eduService.entity.EduVideo;
import com.atguigu.eduService.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author 亚当
 * @since 2022-06-24
 */
@RestController
@RequestMapping("/eduService/video")
@CrossOrigin
public class EduVideoController {

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private VodClient vodClient;

    //添加小节
    @PostMapping("addVideo")
    public R addVideo(@RequestBody EduVideo eduVideo){
        videoService.save(eduVideo);
        return R.ok();
    }
    //删除小节
    //TODO 后面这个方法需要完善，删除小节的时候需要删除里面视频
    @DeleteMapping("{id}")
    public R deleteVideo(@PathVariable String id){
        //线根据小节id获取到视频id再删除
        EduVideo eduVideo = videoService.getById(id);
        String videoSourceId = eduVideo.getVideoSourceId();
        //判断是否有视频id，如果没有则不删
        if(!StringUtils.isEmpty(videoSourceId)){
            R re = vodClient.removeAliyunVideo(videoSourceId);
            System.out.println("ew::::::::::::::"+re);
            if(re.getCode() == 20001){
                throw new GuliException(20001,"删除视频失败，熔断器。");
            }
        }
        videoService.removeById(id);
        return R.ok();
    }
    //修改小节
    @PostMapping("updateVideo")
    public R updateVideo(@RequestBody EduVideo eduVideo){
        videoService.updateById(eduVideo);
        return R.ok();
    }
    //根据章节id查询小节
    @GetMapping("getVideo/{chapterId}")
    public R getVideo(@PathVariable String chapterId){
        EduVideo eduVideo = videoService.getById(chapterId);
        return R.ok().data("eduVideo",eduVideo);
    }
}

